package com.xhj.bigfactory.bigFactory.Collection;

import java.util.HashSet;
import java.util.concurrent.CopyOnWriteArraySet;
/**
 * -
 *
 * @author 见见 pure_xhj@163.com @Date 2021-07-27 @Time 9:46
 */
public class HashSetDemo {
  public static void main(String[] args) {
    //		Set<String> hashSet = new HashSet<>();
    //		Set<String> hashSet = Collections.synchronizedSet(new HashSet<String>());
    CopyOnWriteArraySet<String> hashSet = new CopyOnWriteArraySet(new HashSet<String>());
    new Thread(
            () -> {
              System.out.println(Thread.currentThread().getName() + " come in...");
              hashSet.add("a");
              hashSet.add("b");
              hashSet.add("c");
            },
            "AAA")
        .start();
    new Thread(
            () -> {
              System.out.println(Thread.currentThread().getName() + " come in...");
              hashSet.add("d");
              hashSet.add("e");
              hashSet.add("f");
            },
            "BBB")
        .start();
    for (int i = 0; i < 100; i++) {
      final int k = i;
      new Thread(
              () -> {
                System.out.println(Thread.currentThread().getName() + " come in...");
                hashSet.add(String.valueOf(k));
                hashSet.remove(String.valueOf(k));
                hashSet.add(String.valueOf(k));
              },
              "t" + i)
          .start();
    }
  }
}
